<?php
/**
 * Created by PhpStorm.
 * User: fengbob
 * Date: 2018/2/14
 * Time: 15:41
 */

namespace app\models;


use app\librarys\Tools;
use app\models\db\Category;
use yii\db\Exception;
use yii\db\Query;

class CategoryExt extends Category
{
    public static function getCategory($mini_program, $category_id)
    {

//        $data = Category::find()
//            ->where(['mini_programs_id' => $mini_program, "is_show" => 1])
//            ->andfilterWhere(['parent_id' => $category_id])
//            ->orderBy(['sort' => SORT_DESC])
//            ->all();
//
//        foreach ($data as $ca) {
//            $result[] = $ca->attributes;
//        }
        $data = (new Query())
            ->select(['t1.*','t2.url as image_url'])
            ->from('t_goods_category as t1')
            ->leftJoin('t_file as t2','t1.image_id = t2.id')
            ->where(['t1.mini_programs_id'=>$mini_program,'t1.is_show'=>1,'t1.is_deleted'=>0])
            ->andFilterWhere(['t1.parent_id'=>$category_id])
            ->orderBy(['sort'=>SORT_DESC])
            ->all();
//        if ($data == null)
//            $result = '';
        return $data;
    }


    public static function setCategoryGoods($goods_ids,$category_id){
//        $category = Category::findOne($category_id);
        $sql = 'insert into t_goods_category_goods(id,goods_category_id,goods_id)VALUES (:id,:goods_category_id,:goods_id)';
        $result = true;
        foreach ($goods_ids as $goods_id){
            try {
                $command = \Yii::$app->getDb()->createCommand($sql);
                $nid = Tools::db_uuid();
                $command->bindValue(':id', $nid);
                $command->bindValue(':goods_category_id', $category_id);
                $command->bindValue(':goods_id', $goods_id);
                $result = $command->execute();
            }catch (Exception $e){
                $result = false;
            }
        }
        return $result;

    }


    public static function getCategoryTree($mini_program)
    {
        $query = (new Query())
            ->select('*')
            ->from('t_goods_category')
            ->where(['mini_programs_id' => $mini_program, "is_show" => 1, 'parent_id' => '0'])
            ->all();

        foreach ($query as &$root) {
            $query1 = (new Query())
                ->select('*')
                ->from('t_goods_category')
                ->where(['mini_programs_id' => $mini_program, "is_show" => 1, 'parent_id' => $root['id']])
                ->all();
            if (!empty($query1)) {
                $root['sub'] = $query1;
            }
        }

        return $query;
    }

    public static function getCategoryTreeTable($mini_program)
    {
        $data = [];
        self::_getCategoryTreeTable($data, $mini_program,0, 1);
        return $data;

    }

    public static function _getCategoryTreeTable(&$data, $mini_program, $parent_id, $level)
    {
        $query = (new Query())
            ->select('*')
            ->from('t_goods_category')
            ->where(['mini_programs_id' => $mini_program, "is_enabled" => 1,"is_deleted" => 0, 'parent_id' => $parent_id])
            ->all();

        foreach ($query as $q) {
            $q['level'] = $level;
            array_push($data, $q);
            self::_getCategoryTreeTable($data, $mini_program, $q['id'],$level + 1);
        }
    }


    public static function getCategoryById($id)
    {
        $item = db\Category::find()
            ->where(['id' => $id])
            ->one();
        return $item;
    }

    public static function saveCategory($mini_programs_id,$id,$code,$name,$parent_id,$image_id,$is_show,$sort,$is_enabled)
    {
        $item = self::getCategoryById($id);
        if (!$item) {
            $item = new db\Category();
            $item->id = Tools::db_uuid();
            $item->is_deleted = 0;
        }

        $item->name = $name;
        $item->code = $code;
        $item->parent_id = $parent_id;
        $item->is_show = $is_show;
        $item->mini_programs_id = $mini_programs_id;
        if($image_id) {
            $item->image_id = $image_id;
        }
        $item->is_enabled = $is_enabled;
        $item->sort = $sort;

        $result = $item->save();
        return $result;
    }

    public static function deleteCategory($id)
    {
        $item = self::getCategoryById($id);
        $item->is_deleted = 1;

        $result = $item->save();

        return $result;
    }
}